{% extends "rules/base.html" %}
{% block sidebar %}
    <div class="panel-heading">
        <h2 class="panel-title">{{ source.name }}</h2>
    </div>
    <ul class="panel-body">
        {% if source.descr %}
            <li>
                <span class="type">Description:</span>
                {{ source.descr }}
            </li>
        {% endif %}
        <li>
            <span class="type">Created:</span>
            {{ source.created_date }}
        </li>
        {% if source.updated_date %}
            <li>
                <span class="type">Updated:</span>
                {{ source.updated_date }}
            </li>
        {% endif %}
        <li>
            <span class="type">Use IP reputation:</span>
            {{ source.use_iprep }}
        </li>
    </ul>
    <div class="panel-heading">
        <h2 class="panel-title">Action{% if perms.rules.ruleset_update_push or perms.rules.source_edit %}s{% endif %}</h2>
    </div>
    <ul class="action panel-body">
        <li>
            <a href="{% url 'changelog_source' source.id %}">Changelog</a>
        </li>
        {% if perms.rules.ruleset_update_push %}
            <li>
                <span id="update_source">
                    <a style="cursor: pointer;">Update</a>
                </span>
            </li>
        {% endif %}
        {% if perms.rules.source_edit %}
            <li>
                <a href="{% url 'edit_source' source.id %}">Edit</a>
            </li>
            <li>
                <a href="{% url 'delete_source' source.id %}">Delete</a>
            </li>
        {% endif %}
    </ul>
    {% if perms.rules.source_edit %}
        <script>
    $("#update_source").click(function() {
	$("#update_modal").modal('show');
	$.ajax({
        type:"POST",
        url:"{% url 'update_source' source.id %}",
        success: function(data) {
			if (data['status'] == true) {
				$("#update_modal").modal('hide');
                if (data['redirect'] == true) {
				    window.location = "{% url 'changelog_source' source.id %}";
                }
			} else {
				$("#update_text").text("Error during update: " + data['errors']);
			}
		},
        error: function(data) {
		    $("#update_text").text("Error during update");
        },
        timeout: 120000,
	});
});

        </script>
        <!-- Modal -->
        <div class="modal bs-example-modal-sm"
             id="update_modal"
             tabindex="-1"
             role="dialog"
             aria-labelledby="UpdateModalLabel">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h4 class="modal-title" id="UpdateModalLabel">Please wait...</h4>
                    </div>
                    <div class="modal-body" id="update_text">
                        {% if middleware_has_update %}
                            Triggering source update. . This window will close when operation is registered.
                        {% else %}
                            {{ scirius_short_title }} is updating source. This window will close when update is over.
                        {% endif %}
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    </div>
                </div>
            </div>
        </div>
    {% endif %}
{% endblock sidebar %}
{% block content %}
    {% if diff %}
        <h3 class="title">Changelog ({{ src_update.created_date }})</h3>
        Added: {{ diff.stats.added }}
        Deleted: {{ diff.stats.deleted }}
        Updated: {{ diff.stats.updated }}
        {% load render_table from django_tables2 %}
        {% if diff.stats.added > 0 %}
            <h4 class="title">Added rules</h4>
            {% render_table diff.added %}
        {% endif %}
        {% if diff.stats.deleted > 0 %}
            <h4 class="title">Deleted rules</h4>
            {% render_table diff.deleted %}
        {% endif %}
        {% if diff.stats.updated > 0 %}
            <h4 class="title">Updated rules</h4>
            {% render_table diff.updated %}
        {% endif %}
        {% if changelogs %}
            <h3 class="title">Previous changelog</h3>
            {% render_table changelogs %}
        {% endif %}
    {% elif not error %}
        {% if source.datatype != 'other' %}
            <div class="container-fluid">
                <div class="row">
                    <div class="col-md-6">
                        <h2 class="title">Categories ({{ categories.rows|length }} categories, {{ rules_count }} rules)</h2>
                        {% load render_table from django_tables2 %}
                        {% render_table categories %}
                    </div>
                </div>
            </div>
        {% else %}
            {% if source.method == 'http' %}
                <h2 class="title">Other contents</h2>
                Source fetched from {{ source.uri }}
            {% endif %}
        {% endif %}
    {% endif %}
{% endblock content %}
